home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / Snippets / Development Tools & Languages / MacApp C++ Load⁄dump / DemoTextDump 1 / UDemoText.cp < prev    next >
Encoding:
Text File  |  1992-07-15  |  27.6 KB  |  869 lines  |  [TEXT/MPS ]

  1. // Copyright © 1989-1990 by Apple Computer, Inc.  All rights reserved. 
  2.  
  3.  
  4. #ifndef __DEMOTEXTDUMP__
  5. #include "DemoTextDump.h"
  6. #endif  __DEMOTEXTDUMP__
  7.  
  8. extern "C" char* memcpy(void*, void*, int);
  9. #define pstrcpy(p,q) memcpy(p, q, q[0]+1)                    // added for translation: Pascal strcpy
  10. extern "C" void printf(const char *, ...);
  11.  
  12.     // Menu numbers 
  13. const short mFont                        = 10;
  14.  
  15.     // Command numbers 
  16.  
  17. const short cWidthFrame                 = 601;                // View-width-determination commands
  18. const short cWidthView                    = 602;
  19. const short cWidthOnePage                = 603;
  20.  
  21. const short cHeightFrame                = 604;                // View-height-determination commands
  22. const short cHeightPages                = 605;
  23. const short cHeightText                 = 606;
  24. const short cHeightConst                = 607;
  25.  
  26. const short cJustLeft                    = 608;                // Justification commands
  27. const short cJustCenter                 = 609;
  28. const short cJustRight                    = 610;
  29.  
  30.   /*
  31.  Command numbering scheme:
  32. const short styles = 1000
  33. const short sizes = 1100
  34. const short just = 1200
  35. const short fonts = 1300
  36. const short hiers = 1400
  37. const short colors = 1500
  38.   */
  39.  
  40.     // Command numbers for typestyle attributes
  41. const short cPlainText                    = 1001;
  42. const short cBold                        = 1002;
  43. const short cItalic                     = 1003;
  44. const short cUnderline                    = 1004;
  45. const short cOutline                    = 1005;
  46. const short cShadow                     = 1006;
  47. const short cCondense                    = 1007;
  48. const short cExtend                     = 1008;
  49.  
  50.     // Command numbers for font-size commands
  51. const short cSizeChange                 = 1100;
  52. const short cSizeBase                    = 1100;
  53. const short cSizeMin                    = 1109;
  54. const short cSizeMax                    = 1124;
  55.     // 1101-1197 reserved for font sizes 1-97 pts.
  56. const short cSizeGrow                    = 1198;
  57. const short cSizeShrink                 = 1199;
  58.  
  59.     // Command numbers to cover other stylistic changes
  60. const short cJustChange                 = 1200;
  61. const short cFontChange                 = 1300;
  62.  
  63.     // Command numbers for the hierarchial menu
  64. const short cStyle                        = 1401;
  65. const short cSize                        = 1402;
  66. const short cFont                        = 1403;
  67. const short cColor                        = 1404;
  68.  
  69.     // Command numbers for changing colors
  70. const short cColorChange                = 1500;
  71. const short cColorText                    = 1501;
  72. const short cColorBackground            = 1502;
  73.  
  74. const short cXShowBreaks                = 1601;
  75.  
  76.     // Constant for staggering windows
  77. const short kStaggerAmount                = 16;
  78.  
  79.     // Constant for amount to relative size text selection
  80. const short kRelSizeAmount                = 4;
  81.  
  82.     // Constants for the text specs resource
  83. const unsigned long kTextSpecsRsrcType    = 'SPEC';
  84. const short kTextSpecsRsrcID            = 1;
  85.  
  86.     // Constants for the text style resource
  87. const unsigned long kTextStyleRsrcType    = 'STYL';
  88. const short kStylesRsrcID                = 1;
  89. const short kElementsRsrcID             = 2;
  90.  
  91.     // Constants for the print info resource
  92. const unsigned long kPrintInfoRsrcType    = 'PRNT';
  93. const short kPrintInfoRsrcID            = 1;
  94.  
  95.     // Constants for the prompts string list
  96. const short kPromptsRsrcID                = 1001;
  97. const short kColTextPrompt                = 1;
  98. const short kColBackPrompt                = 2;
  99.  
  100.     // The 'File is too large' alert
  101. const short kFileTooBig                 = 1000;
  102.  
  103. const short kHierDisplayedMBar            = 131;            // Displayed menus on hier. menu system
  104. const short kNonHierDisplayedMBar         = 128;            // Displayed menus on non-hier. system
  105.  
  106. const short kHierMenuOffset             = 1000;         // Offset added to non-hier menu cmds to get
  107.                                                         //  right #
  108.  
  109. TextSpecs    gDefaultSpecs;                                // Text specs as default for new documents
  110. short        gStaggerCount;
  111. short        gMenuOfs;
  112. Str255        gPromptString;
  113.  
  114. //--------------------------------------------------------------------------------------------------
  115. #pragma segment ARes
  116.  
  117. StringPtr GetPrompt(short index)
  118. {
  119.     GetIndString(gPromptString, kPromptsRsrcID, index);
  120.     return (StringPtr) &gPromptString;
  121. }
  122.  
  123. //******************************************************************************************
  124. //  T D e m o T e x t A p p l i c a t i o n
  125. //******************************************************************************************
  126. //--------------------------------------------------------------------------------------------------
  127. #pragma segment AInit
  128.  
  129. pascal void TDemoTextApplication::IDemoTextApplication()
  130. {
  131.     
  132.     Str255    fontName;
  133.     TTEView    *aTEView;
  134.  
  135. #if qNeedsHierarchialMenus == 0
  136.     if (! gConfiguration.hasHierarchicalMenus) {
  137.         gMBarDisplayed = kNonHierDisplayedMBar;
  138.         gMenuOfs = 0;
  139.     } else
  140. #endif
  141.     {
  142.         gMBarDisplayed = kHierDisplayedMBar;
  143.         gMenuOfs = kHierMenuOffset;
  144.     }
  145.  
  146.     IApplication(kFileType);
  147.  
  148.     if (! gFinderPrinting) {
  149.         AddResMenu(GetMHandle(mFont), 'FONT');
  150.  
  151.         gStaggerCount = 0;
  152.  
  153.         SetStyle(cBold, bold);
  154.         SetStyle(cUnderline, underline);
  155.         SetStyle(cItalic, italic);
  156.         SetStyle(cOutline, outline);
  157.         SetStyle(cShadow, shadow);
  158.         SetStyle(cCondense, condense);
  159.         SetStyle(cExtend, extend);
  160.     }
  161.  
  162.     // Instead of hard-wiring a default text style, get it from the TEView resource by
  163.     aTEView = (TTEView*) DoCreateViews(NULL, NULL, kViewRsrcID, &gZeroVPt);
  164.     FailNIL(aTEView);
  165.     GetFontName(aTEView->fTextStyle.tsFont, fontName);
  166.     //gDefaultSpecs.theTextFont = fontName;                        // Set up initial text specs
  167.     pstrcpy(gDefaultSpecs.theTextFont, fontName);                // Kludged Pascal string copy
  168.     gDefaultSpecs.theTextFace = aTEView->fTextStyle.tsFace;
  169.     gDefaultSpecs.theTextSize = aTEView->fTextStyle.tsSize;
  170.     gDefaultSpecs.theTextColor = aTEView->fTextStyle.tsColor;
  171.     gDefaultSpecs.theJustification = aTEView->fJustification;
  172.     gDefaultSpecs.theBackColor = gRGBWhite;
  173.     aTEView->Free();
  174. }
  175.  
  176. //--------------------------------------------------------------------------------------------------
  177. #pragma segment AOpen
  178.  
  179. pascal TDocument *TDemoTextApplication::DoMakeDocument(CmdNumber )
  180. {
  181.     TTextDocument    *aTextDocument;
  182.  
  183.     aTextDocument = new TTextDocument;
  184.     FailNIL(aTextDocument);
  185.     aTextDocument->ITextDocument();
  186.     return aTextDocument;
  187. }
  188.  
  189. //--------------------------------------------------------------------------------------------------
  190. #if qDebug
  191. #pragma segment ADebug
  192.  
  193. pascal void TDemoTextApplication::IdentifySoftware()
  194. {
  195.     printf("DemoText Source date: 23 April 86; Compiled: %s @ %s\n", __DATE__, __TIME__);
  196.     inherited::IdentifySoftware();
  197. }
  198. #endif
  199.  
  200. //******************************************************************************************
  201. //  T T e x t D o c u m e n t
  202. //******************************************************************************************
  203. //--------------------------------------------------------------------------------------------------
  204. #pragma segment AOpen
  205.  
  206. pascal void TTextDocument::ITextDocument()
  207. {
  208.     fDocText = NULL;
  209.     IDocument(kFileType, kSignature, kUsesDataFork, kUsesRsrcFork, ! kDataOpen, ! kRsrcOpen);
  210.  
  211.     fTEView = NULL;
  212.     fStyles = NULL;
  213.     fElements = NULL;
  214.     fDocText = NewPermHandle(0);
  215.     FailNIL(fDocText);
  216. }
  217.  
  218. //--------------------------------------------------------------------------------------------------
  219. #pragma segment AClose
  220.  
  221. pascal void TTextDocument::Free()
  222. {
  223.     if (fDocText)
  224.         DisposHandle(fDocText);
  225.     inherited::Free();
  226. }
  227.  
  228. //--------------------------------------------------------------------------------------------------
  229. #pragma segment ANonRes
  230.  
  231. pascal void TTextDocument::ChangeBackColor(RGBColor *newColor)
  232. {
  233.     GrafPtr    oldPort;
  234.     TWindow    *itsWindow;
  235.  
  236.  // ??? Is this right?  Should we set the background color even if the view isn't
  237.  //  in a window yet?
  238.     GetPort(&oldPort);
  239.     SetPort((*fTEView->fHTE)->inPort);
  240.     RGBBackColor(newColor);
  241.     itsWindow = fTEView->GetWindow();
  242.     if (itsWindow)
  243.         itsWindow->ForceRedraw();
  244.     SetPort(oldPort);
  245. }
  246.  
  247. //--------------------------------------------------------------------------------------------------
  248. #pragma segment AOpen
  249.  
  250. pascal void TTextDocument::DoInitialState()
  251. {
  252.     fTextSpecs = gDefaultSpecs;
  253. }
  254.  
  255. //--------------------------------------------------------------------------------------------------
  256. #pragma segment AOpen
  257.  
  258. pascal void TTextDocument::DoMakeViews(Boolean forPrinting)
  259. {
  260.     TView                *aView;
  261.     TStdPrintHandler    *aHandler;
  262.  
  263.     if (forPrinting)
  264.     // We're only finder printing--don't need a window, just the view being printed 
  265.         aView = DoCreateViews(this, NULL, kViewRsrcID, &gZeroVPt);
  266.     else
  267.         aView = NewTemplateWindow(kWindowRsrcID, (TDocument*) this);
  268.  
  269.     FailNIL(aView);                                     // ??? Will we have already failed?
  270.     aView = aView->FindSubView('TEVW');
  271.     fTEView = (TTEView*) aView;                            // Must cast because FindSubView returns
  272.                                                         /// TView 
  273.     aHandler = new TStdPrintHandler;
  274.     FailNIL(aHandler);
  275.     aHandler->IStdPrintHandler((TDocument*) this,         // its document
  276.                               (TView*) fTEView,            // its view
  277.                               0,                        // does not have square dots
  278.                               1,                         // horzontal page size is fixed
  279.                               0);                        // vertical page size is variable (could be
  280.                                                         //  set
  281.     // …to true on non-style TE systems)
  282.     aHandler->fMinimalMargins = 0;
  283.     ShowReverted();
  284. }
  285.  
  286. //--------------------------------------------------------------------------------------------------
  287. #pragma segment ASelCommand
  288.  
  289. static SizeDeterminer    sd;                // for shared use by the following functions
  290. static Str255            aName;            // which are used by TTextDocument::DoMenuCommand()
  291. static short            menu;
  292. static short            item;
  293. static TextStyle        newStyle;
  294. static CmdNumber        aCmdNumber;
  295. static TTextDocument    *ttd;            // kludged copy of "this"
  296.  
  297. //--------------------------------------------------------------------------------------------------
  298.  
  299. pascal static TCommand *DoSizeChange(CmdNumber base)
  300. {
  301.     newStyle.tsSize = aCmdNumber - base;
  302.     ttd->fTextSpecs.theTextSize = newStyle.tsSize;
  303.     return ttd->fTEView->DoMakeStyleCommand(&newStyle, cSizeChange, doSize + doToggle);
  304. }
  305.  
  306. //--------------------------------------------------------------------------------------------------
  307.  
  308. pascal static TCommand *DoRelSizeChange(short amount)
  309. {
  310.     newStyle.tsSize = amount;
  311.     ttd->fTextSpecs.theTextSize += amount;
  312.     return ttd->fTEView->DoMakeStyleCommand(&newStyle, cSizeChange, doSize + addSize + doToggle);
  313. }
  314.  
  315. //--------------------------------------------------------------------------------------------------
  316.  
  317. pascal static TCommand *DoFontChange()
  318. {
  319.     GetItem(GetMHandle(menu), item, aName);
  320.     GetFNum(aName, &newStyle.tsFont);
  321.     //ttd->fTextSpecs.theTextFont = aName;
  322.     pstrcpy(ttd->fTextSpecs.theTextFont, aName);        // kludged Pascal string copy
  323.     return ttd->fTEView->DoMakeStyleCommand(&newStyle, cFontChange, doFont + doToggle);
  324. }
  325.  
  326. //--------------------------------------------------------------------------------------------------
  327.  
  328. pascal static TCommand *DoColTextChange()
  329. {
  330.     RGBColor    aColor;
  331.     static Point P4040 = {0x0040, 0x0040};
  332.  
  333.     aColor = ttd->fTextSpecs.theTextColor;
  334.     if (GetColor(P4040, GetPrompt(kColTextPrompt), &aColor, &newStyle.tsColor)) {
  335.         ttd->fTextSpecs.theTextColor = newStyle.tsColor;
  336.         return ttd->fTEView->DoMakeStyleCommand(&newStyle, cColorChange, doColor + doToggle);
  337.     }
  338.     return gNoChanges;
  339. }
  340.  
  341. //--------------------------------------------------------------------------------------------------
  342.  
  343. pascal static void DoColBackChange()
  344. {
  345.     RGBColor    aColor;
  346.     static Point P4040 = {0x0040, 0x0040};
  347.  
  348.     aColor = ttd->fTextSpecs.theBackColor;
  349.     if (GetColor(P4040, GetPrompt(kColBackPrompt), &aColor, &newStyle.tsColor)) {
  350.         ttd->fTextSpecs.theBackColor = newStyle.tsColor;
  351.         ttd->ChangeBackColor(&newStyle.tsColor);
  352.     }
  353. }
  354.  
  355. //--------------------------------------------------------------------------------------------------
  356.  
  357. pascal static TCommand *DoJustChange()
  358. {
  359.     short            newJust;
  360.     TJustCommand    *aJustChange;
  361.  
  362.     switch (aCmdNumber) {
  363.         case cJustLeft:        newJust = teJustLeft;    break;
  364.         case cJustCenter:    newJust = teJustCenter;    break;
  365.         case cJustRight:    newJust = teJustRight;    break;
  366.     }
  367.     aJustChange = new TJustCommand;
  368.     FailNIL(aJustChange);
  369.     aJustChange->IJustCommand(ttd->fTEView, newJust);
  370.     ttd->fTextSpecs.theJustification = newJust;
  371.     return aJustChange;
  372. }
  373.  
  374. //--------------------------------------------------------------------------------------------------
  375.  
  376. pascal static TCommand *DoPlainChange()
  377. {
  378.     newStyle.tsFace = 0;
  379.     ttd->fTextSpecs.theTextFace = 0;
  380.     return ttd->fTEView->DoMakeStyleCommand(&newStyle, cStyleChange, doFace);
  381. }
  382.  
  383. //--------------------------------------------------------------------------------------------------
  384.  
  385. pascal static TCommand *DoStyleChange()
  386. {
  387.     switch (aCmdNumber) {
  388.         case cBold:            newStyle.tsFace = bold;            break;
  389.         case cItalic:        newStyle.tsFace = italic;        break;
  390.         case cUnderline:    newStyle.tsFace = underline;    break;
  391.         case cOutline:        newStyle.tsFace = outline;        break;
  392.         case cShadow:        newStyle.tsFace = shadow;        break;
  393.         case cCondense:        newStyle.tsFace = condense;        break;
  394.         case cExtend:        newStyle.tsFace = extend;        break;
  395.     }
  396.     if (newStyle.tsFace * ttd->fTextSpecs.theTextFace == newStyle.tsFace)
  397.         ttd->fTextSpecs.theTextFace -= newStyle.tsFace;
  398.     else
  399.         ttd->fTextSpecs.theTextFace += newStyle.tsFace;
  400.     return ttd->fTEView->DoMakeStyleCommand(&newStyle, cStyleChange, doFace + doToggle);
  401. }
  402.  
  403. //--------------------------------------------------------------------------------------------------
  404.  
  405. pascal static void InstallChangedDeterminer(VHSelect vhs)
  406. {
  407.     if (sd != ttd->fTEView->fSizeDeterminer[vhs]) {
  408.         ttd->fTEView->fSizeDeterminer[vhs] = sd;
  409.  
  410. // If we changed the horizontal size determiner, we must
  411. // ask the TTEView to recompute the TE rectangles.
  412.         if (vhs == h) {
  413.             if (sd == sizeSuperView)
  414.                 ttd->fTEView->SuperViewChangedSize(&gZeroVPt, 1); // Don't care about deltas
  415.             else if (sd == sizePage)
  416.                 ttd->fTEView->DoPagination();
  417.         }
  418.  
  419.         ttd->fTEView->AdjustSize();
  420.         ttd->fTEView->ForceRedraw();
  421.     }
  422. }
  423.  
  424. //--------------------------------------------------------------------------------------------------
  425.  
  426. pascal static void DoWidthChange()
  427. {
  428.     if (aCmdNumber == cWidthFrame)
  429.         sd = sizeSuperView;
  430.     else if (aCmdNumber == cWidthOnePage)
  431.         sd = sizePage;
  432.     else
  433.         sd = sizeFixed;
  434.  
  435.     InstallChangedDeterminer(h);                    // NB: The following is not undoable in the
  436.                                                     // current version
  437. }
  438.  
  439. //--------------------------------------------------------------------------------------------------
  440.  
  441. pascal static void DoHeightChange()
  442. {
  443.     switch (aCmdNumber) {
  444.         case cHeightFrame:    sd = sizeSuperView;    break;
  445.         case cHeightPages:    sd = sizeFillPages;    break;
  446.         case cHeightText:    sd = sizeVariable;    break;
  447.         case cHeightConst:    sd = sizeFixed;        break;
  448.     }
  449.  
  450.     InstallChangedDeterminer(v);                    // NB: The following is not undoable in the
  451.                                                     // current version
  452. }
  453.  
  454. pascal TCommand *TTextDocument::DoMenuCommand(CmdNumber theCmdNumber)
  455. {
  456.     aCmdNumber = theCmdNumber;        // for use in formerly nested procedures
  457.     ttd = this;                        // ditto
  458.  
  459.     CmdToMenuItem(aCmdNumber, &menu, &item);
  460.  
  461.     if (menu == mFont) {
  462.         return DoFontChange();
  463.     } else {
  464.         if (aCmdNumber >= cSizeMin && aCmdNumber <= cSizeMax)
  465.             return DoSizeChange(cSizeBase);
  466.         else if (aCmdNumber == cSizeGrow)
  467.             return DoRelSizeChange(kRelSizeAmount);
  468.         else if (aCmdNumber == cSizeShrink)
  469.             return DoRelSizeChange( - kRelSizeAmount);
  470.         else if (aCmdNumber >= cJustLeft && aCmdNumber <= cJustRight)
  471.             return DoJustChange();
  472.         else if (aCmdNumber == cPlainText)
  473.             return DoPlainChange();
  474.         else if (aCmdNumber >= cBold && aCmdNumber <= cExtend)
  475.             return DoStyleChange();
  476.         else if (aCmdNumber == cColorText)
  477.             return DoColTextChange();
  478.         else if (aCmdNumber == cColorBackground)
  479.             DoColBackChange();
  480.         else if (aCmdNumber >= cWidthFrame && aCmdNumber <= cWidthOnePage)
  481.             DoWidthChange();
  482.         else if (aCmdNumber >= cHeightFrame && aCmdNumber <= cHeightConst)
  483.             DoHeightChange();
  484.             // ??? Next line has got to change ??????????
  485.         else if (aCmdNumber == cXShowBreaks)
  486.             return fDocPrintHandler->DoMenuCommand(cShowBreaks);
  487.         else
  488.             return inherited::DoMenuCommand(aCmdNumber);
  489.     }
  490.     return gNoChanges;
  491. }
  492.  
  493. //--------------------------------------------------------------------------------------------------
  494. #pragma segment AWriteFile
  495.  
  496. pascal void TTextDocument::DoNeedDiskSpace(long  *dataForkBytes, long *rsrcForkBytes)
  497. {
  498.     TEStyleHandle    styles;
  499.     STHandle        elements;
  500.  
  501.     *dataForkBytes += GetHandleSize(fDocText);
  502.  
  503.     *rsrcForkBytes += kPrintInfoSize + kRsrcTypeOverhead + kRsrcOverhead;
  504.  
  505.     if (fTEView->fStyleType == kWithStyle && fTEView->fHTE) {
  506.         fTEView->ExtractStyles(&styles, &elements);
  507.         *rsrcForkBytes += GetHandleSize(Handle(styles)) + kRsrcTypeOverhead +
  508.                          kRsrcOverhead + GetHandleSize(Handle(elements)) + kRsrcTypeOverhead +
  509.                          kRsrcOverhead;
  510.     }
  511.  
  512.     *rsrcForkBytes += sizeof(TextSpecs) + kRsrcTypeOverhead + kRsrcOverhead;
  513.  
  514.     // Get resource file overhead 
  515.     inherited::DoNeedDiskSpace(dataForkBytes, rsrcForkBytes);
  516. }
  517.  
  518. //--------------------------------------------------------------------------------------------------
  519. #pragma segment AReadFile
  520.  
  521. pascal void TTextDocument::DoRead(short aRefNum, Boolean , Boolean )
  522. {
  523.     long            numChars;
  524.     TextSpecsHdl    hTextSpecs;
  525.     Handle            hPrintInfo;
  526.  
  527.     FailOSErr(GetEOF(aRefNum, &numChars));            // Read in the text 
  528.  
  529.     if (numChars > kUnlimited) {                            // The file may have been created by…
  530.         gApplication->ShowError(0, msgAlert + kFileTooBig);    // …someone else - limit it to 32K!
  531.         numChars = kUnlimited;
  532.     }
  533.  
  534.     SetHandleSize(fDocText, numChars);
  535.     FailMemError();
  536.     FailOSErr(FSRead(aRefNum, &numChars, *fDocText));
  537.  
  538.     fStyles = TEStyleHandle(GetResource(kTextStyleRsrcType, kStylesRsrcID)); // Read in the TEStyleRec
  539.     if (fStyles)
  540.         DetachResource(Handle(fStyles));
  541.  
  542.     fElements =                                            // Read in the STElement array
  543.       STHandle(GetResource(kTextStyleRsrcType, kElementsRsrcID));
  544.     if (fElements)
  545.         DetachResource(Handle(fElements));
  546.  
  547.     hTextSpecs =                                        // Read the text specs resource
  548.       TextSpecsHdl(GetResource(kTextSpecsRsrcType, kTextSpecsRsrcID));
  549.     if (hTextSpecs)
  550.         fTextSpecs = **hTextSpecs;
  551.     else
  552.         DoInitialState();
  553.  
  554.     hPrintInfo =                                        // Read the print info resource
  555.       GetResource(kPrintInfoRsrcType, kPrintInfoRsrcID);
  556.     if (hPrintInfo) {                                    // no print info resources was saved
  557.         if (fPrintInfo == NULL) {
  558.             fPrintInfo = NewPermHandle(kPrintInfoSize);
  559.             FailNIL(fPrintInfo);
  560.         }
  561.         BlockMove(*hPrintInfo, *fPrintInfo, kPrintInfoSize);
  562.     }
  563. }
  564.  
  565. //--------------------------------------------------------------------------------------------------
  566. #pragma segment ARes
  567.  
  568. pascal void TTextDocument::DoSetupMenus()
  569. {
  570.     Boolean            hasColor;
  571.     Boolean            hasStyle;
  572.     Boolean            checkPlain;
  573.     Boolean            checkSize;
  574.     Boolean            checkFont;
  575.     Boolean            specChange;
  576.     short            just;
  577.     short            item;
  578.     short            fnt;
  579.     short            c;
  580.     short            aMode;
  581.     Style            aFace;
  582.     SizeDeterminer    sd;
  583.     MenuHandle        aMenuHandle;
  584.     Str255            aName;
  585.     TextStyle        aStyle;
  586.     short            theFont;
  587.     short            startOfSelection, endOfSelection;
  588.     Str255            aStr255;
  589.  
  590.     inherited::DoSetupMenus();
  591.  
  592.     hasColor = gConfiguration.hasColorQD;
  593.     hasStyle = gConfiguration.hasStyleTextEdit;
  594.  
  595. #if qNeedsStyleTextEdit == 0
  596.     if (! hasStyle) {
  597.         //aStr255 = fTextSpecs.theTextFont;
  598.         pstrcpy(aStr255, fTextSpecs.theTextFont);        // kludged Pascal string copy
  599.         GetFNum(aStr255, &aStyle.tsFont);
  600.         aStyle.tsFace = fTextSpecs.theTextFace;
  601.         aStyle.tsSize = fTextSpecs.theTextSize;
  602.         aStyle.tsColor = fTextSpecs.theTextColor;
  603.         checkPlain = aStyle.tsFace = 0;
  604.         checkFont = 1;
  605.     } else
  606. #endif
  607.     {
  608.         startOfSelection = (*fTEView->fHTE)->selStart;
  609.         endOfSelection = (*fTEView->fHTE)->selEnd;
  610.         aMode = doFace;
  611.         checkPlain = fTEView->ContinuousStyle(startOfSelection, endOfSelection, &aMode, &aStyle) &&
  612.                       (aStyle.tsFace == 0);
  613.  
  614.         aMode = doAll;
  615.         aStyle.tsFace = bold | italic | underline | outline | shadow | extend | condense;
  616.         specChange = fTEView->ContinuousStyle(startOfSelection, endOfSelection, &aMode, &aStyle);
  617.         checkFont = ((aMode & doFont) != 0);
  618.     }
  619.  
  620.     aMenuHandle = GetMHandle(mFont);
  621.     // If specs have changed AND EITHER an old-style record OR the font's continuous - Ed.
  622.     specChange = fTEView->fSpecsChanged && ((! hasStyle) || checkFont);
  623.  
  624.     GetFontName(aStyle.tsFont, aName);                    // Get real font number in case tsFont is
  625.     GetFNum(aName, &theFont);                            // …the system or application font.
  626.     for (item=1; item <= CountMItems(aMenuHandle); item++) {
  627.   // There can be more than 31 menu entries with scrolling menus, but trying to enable
  628.   // an item with number > 31 is bad news.  If the menu itself is enabled (which it
  629.   // will be in MacApp if any of the first 31 items is enabled), then the extras
  630.   // will always be enabled.
  631.         if (item <= 31)
  632.             EnableItem(aMenuHandle, item);
  633.         if (checkFont) {
  634.             GetItem(aMenuHandle, item, aName);
  635.             GetFNum(aName, &fnt);
  636.             CheckItem(aMenuHandle, item, fnt == theFont);
  637.         }
  638.     }
  639.  
  640.     EnableCheck(cXShowBreaks, 1, ((TStdPrintHandler*)fDocPrintHandler)->fShowBreaks);
  641.  
  642.     sd = fTEView->fSizeDeterminer[h];                    // Enable size determiner related menu items
  643.  
  644.     EnableCheck(cWidthFrame, 1, (sd == sizeSuperView));
  645.     EnableCheck(cWidthOnePage, 1, (sd == sizePage));
  646.     EnableCheck(cWidthView, 1, (sd == sizeFixed));
  647.  
  648.     sd = fTEView->fSizeDeterminer[v];
  649.     EnableCheck(cHeightFrame, 1, (sd == sizeSuperView));
  650.     EnableCheck(cHeightPages, 1, (sd == sizeFillPages));
  651.     EnableCheck(cHeightText, 1, (sd == sizeVariable));
  652.     EnableCheck(cHeightConst, 1, (sd == sizeFixed));
  653.  
  654.     just = fTEView->fJustification;                     // Enable justification related menu items
  655.     EnableCheck(cJustLeft, 1, (just == teJustLeft));
  656.     EnableCheck(cJustCenter, 1, (just == teJustCenter));
  657.     EnableCheck(cJustRight, 1, (just == teJustRight));
  658.  
  659. #if qNeedsHierarchialMenus == 0
  660.     if (gConfiguration.hasHierarchicalMenus)
  661. #endif
  662.     {
  663.         Enable(cStyle, 1);                                //  Enable sub-menus
  664.         Enable(cSize, 1);
  665.         Enable(cFont, 1);
  666.         Enable(cColor, hasColor);
  667.     }
  668.  
  669.     aFace = aStyle.tsFace;
  670.     
  671.     EnableCheck(cPlainText, 1, checkPlain);                // Enable normal Style menu items
  672.     EnableCheck(cBold, 1,      aFace & bold);
  673.     EnableCheck(cItalic, 1,    aFace & italic);
  674.     EnableCheck(cUnderline, 1, aFace & underline);
  675.     EnableCheck(cOutline, 1,   aFace & outline);
  676.     EnableCheck(cShadow, 1,    aFace & shadow);
  677.     EnableCheck(cCondense, 1,  aFace & condense);
  678.     EnableCheck(cExtend, 1,    aFace & extend);
  679.  
  680.     for (c=cSizeMin; c<=cSizeMax; c++) {
  681.         if (hasStyle && ((aMode & doSize) == 0))
  682.             checkSize = 0;
  683.         else
  684.             checkSize = (c - cSizeBase) == aStyle.tsSize;
  685.         EnableCheck(c, 1, checkSize);
  686.         if (fTEView->fSpecsChanged) {
  687.             if ( ((! hasStyle) ||                        // If the record isn't styled, or
  688.                  (aMode & doFont)) &&                    // …it _is_ styled and font is continuous
  689.                RealFont(aStyle.tsFont,c-cSizeBase) )    // …and the size is a real one
  690.                 aFace = outline;                        // …then we outline it
  691.             else
  692.                 aFace = 0;
  693.             SetStyle(c, aFace);
  694.         }
  695.     }
  696.  
  697.     Enable(cSizeGrow, 1);
  698.     Enable(cSizeShrink, 1);
  699.  
  700.     Enable(cColorText, hasColor);
  701.     Enable(cColorBackground, hasColor);
  702.  
  703.     fTEView->fSpecsChanged = 0;
  704. }
  705.  
  706. //--------------------------------------------------------------------------------------------------
  707. #pragma segment AWriteFile
  708.  
  709. pascal void TTextDocument::DoWrite(short aRefNum, Boolean )
  710. {
  711.     long            numChars;
  712.     TextSpecsHdl    hTextSpecs;
  713.     Handle            tempHandle;
  714.     TEStyleHandle    styles;
  715.     STHandle        elements;
  716.  
  717.     // Write out the text
  718.     numChars = GetHandleSize(fDocText);
  719.     FailOSErr(FSWrite(aRefNum, &numChars, *fDocText));
  720.  
  721.     if (fTEView->fStyleType == kWithStyle) {
  722.         fTEView->ExtractStyles(&styles, &elements);
  723.         FailOSErr(HandToHand((Handle*)&styles));
  724.         AddResource(Handle(styles), kTextStyleRsrcType, kStylesRsrcID, "\p");
  725.         FailResError();
  726.         FailOSErr(HandToHand((Handle*)&elements));
  727.         AddResource(Handle(elements), kTextStyleRsrcType, kElementsRsrcID, "\p");
  728.         FailResError();
  729.     }
  730.  
  731.     // Write the text specification resource, after converting it to a handle
  732.     hTextSpecs = TextSpecsHdl(NewHandle(sizeof(TextSpecs)));
  733.     FailNIL(hTextSpecs);
  734.     **hTextSpecs = fTextSpecs;
  735.     AddResource(Handle(hTextSpecs), kTextSpecsRsrcType, kTextSpecsRsrcID, "\p");
  736.  
  737.     FailResError();
  738.  
  739.  // Write the print info resource.  Note we can't use MacApp for this because MacApp
  740.  // will write the print info into the data fork.  Note also--we must copy the print
  741.  // info resource to another handle because the Resource Manager will dispose of the
  742.  // resource handles when a resource fork is closed.
  743.     if (fPrintInfo) {                            // Make sure we have one of these guy
  744.         tempHandle = fPrintInfo;
  745.         FailOSErr(HandToHand(&tempHandle));
  746.         AddResource(tempHandle, kPrintInfoRsrcType, kPrintInfoRsrcID, "\p");
  747.         FailResError();
  748.     }
  749. }
  750.  
  751. //--------------------------------------------------------------------------------------------------
  752. #pragma segment AClose
  753.  
  754. pascal void TTextDocument::FreeData()
  755. {
  756.     SetHandleSize(fDocText, 0);
  757. }
  758.  
  759. //--------------------------------------------------------------------------------------------------
  760. #pragma segment ARes
  761.  
  762. pascal void TTextDocument::SetSpecStyle()
  763. {
  764.     short        theFont;
  765.     TextStyle    theTS;
  766.     Str255        aStr255;
  767.  
  768.     //aStr255 = fTextSpecs.theTextFont;
  769.     pstrcpy(aStr255, fTextSpecs.theTextFont);    // kludged Pascal string copy
  770.     GetFNum(aStr255, &theFont);
  771.     SetTextStyle(&theTS, theFont, fTextSpecs.theTextFace,
  772.                      fTextSpecs.theTextSize, &fTextSpecs.theTextColor);
  773.     fTEView->SetOneStyle(0, 0, doAll, &theTS, kDontRedraw);
  774. }
  775.  
  776. //--------------------------------------------------------------------------------------------------
  777. #pragma segment AReadFile
  778.  
  779. pascal void TTextDocument::ShowReverted()
  780. {
  781.     RGBColor    aColor;
  782.  
  783.     fTEView->StuffText(fDocText);                        // put in the text
  784.     TESetSelect(0, 0, fTEView->fHTE);
  785.     if (fStyles && fElements)                            // If we're able, stuff style info
  786.         fTEView->StuffStyles(fStyles, fElements);
  787.     else
  788.         SetSpecStyle();
  789. #if qNeedsColorQD == 0
  790.     if (gConfiguration.hasColorQD)
  791. #endif
  792.     {
  793.         aColor = fTextSpecs.theBackColor;
  794.         ChangeBackColor(&aColor);
  795.     }
  796.     fTEView->SetJustification(fTextSpecs.theJustification, kDontRedraw);
  797.     inherited::ShowReverted();
  798. }
  799.  
  800. //--------------------------------------------------------------------------------------------------
  801. #pragma segment AFields
  802.  
  803. pascal void TTextDocument::Fields(pascal void (*DoToField)(StringPtr fieldName, Ptr fieldAddr,
  804.                             short fieldType, void *DoToField_StaticLink), void *DoToField_StaticLink)
  805. {
  806.     (*DoToField)("\pTTextDocument",    NULL, bClass, DoToField_StaticLink);
  807.     (*DoToField)("\pfDocText",            (Ptr) &fDocText, bHandle, DoToField_StaticLink);
  808.     (*DoToField)("\pfTEView",            (Ptr) &fTEView, bObject, DoToField_StaticLink);
  809.     (*DoToField)("\p   fTextSpecs",    NULL, bClass, DoToField_StaticLink);
  810.     (*DoToField)("\p theTextFont",        (Ptr) &fTextSpecs.theTextFont, bString, DoToField_StaticLink);
  811.     (*DoToField)("\p theTextFace",        (Ptr) &fTextSpecs.theTextFace, bStyle, DoToField_StaticLink);
  812.     (*DoToField)("\p theTextSize",        (Ptr) &fTextSpecs.theTextSize, bInteger, DoToField_StaticLink);
  813.     (*DoToField)("\p theTextColor",    (Ptr) &fTextSpecs.theTextColor, bRGBColor, DoToField_StaticLink);
  814.     (*DoToField)("\p theJustification",(Ptr) &fTextSpecs.theJustification, bInteger, DoToField_StaticLink);
  815.     (*DoToField)("\p theBackColor",    (Ptr) &fTextSpecs.theBackColor, bRGBColor, DoToField_StaticLink);
  816.     inherited::Fields(DoToField, DoToField_StaticLink);
  817. }
  818.  
  819. //******************************************************************************************
  820. //  T J u s t C o m m a n d
  821. //******************************************************************************************
  822. //--------------------------------------------------------------------------------------------------
  823. #pragma segment ASelCommand
  824.  
  825. pascal void TJustCommand::IJustCommand(TTEView *itsTEView, short itsNewJust)
  826. {
  827.     ICommand(cJustChange, itsTEView->fDocument, NULL, NULL);
  828.     fTEView = itsTEView;
  829.     fOldJust = itsTEView->fJustification;
  830.     fNewJust = itsNewJust;
  831. }
  832.  
  833. //--------------------------------------------------------------------------------------------------
  834. #pragma segment ADoCommand
  835.  
  836. pascal void TJustCommand::DoIt()
  837. {
  838.     fTEView->SetJustification(fNewJust, kRedraw);
  839. }
  840.  
  841. //--------------------------------------------------------------------------------------------------
  842. #pragma segment ADoCommand
  843.  
  844. pascal void TJustCommand::RedoIt()
  845. {
  846.     DoIt();
  847. }
  848.  
  849. //--------------------------------------------------------------------------------------------------
  850. #pragma segment ADoCommand
  851.  
  852. pascal void TJustCommand::UndoIt()
  853. {
  854.     fTEView->SetJustification(fOldJust, kRedraw);
  855. }
  856.  
  857. //--------------------------------------------------------------------------------------------------
  858. #pragma segment AFields
  859.  
  860. pascal void TJustCommand::Fields(pascal void (*DoToField)(StringPtr fieldName, Ptr fieldAddr,
  861.                           short fieldType, void *DoToField_StaticLink), void *DoToField_StaticLink)
  862. {
  863.     DoToField("\pTJustCommand",    NULL, bClass, DoToField_StaticLink);
  864.     DoToField("\pfTEView",        (Ptr) &fTEView, bObject, DoToField_StaticLink);
  865.     DoToField("\pfOldJust",        (Ptr) &fOldJust, bInteger, DoToField_StaticLink);
  866.     DoToField("\pfNewJust",        (Ptr) &fNewJust, bInteger, DoToField_StaticLink);
  867.     inherited::Fields(DoToField, DoToField_StaticLink);
  868. }
  869.